<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="keywords" content="技术博客">
    
    <meta name="author" content="jiesun">
    <!-- preconnect -->
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    
    
    <!--- Seo Part-->
    
    <link rel="canonical" href="https://anca.gitee.io/2020/11/22/reactrendercompletework/"/>
    <meta name="robots" content="index,follow">
    <meta name="googlebot" content="index,follow">
    <meta name="revisit-after" content="1 days">
    
        <meta name="description" content="Fiber节点是如何被创建并构建Fiber树之completeWork做的事？">
<meta property="og:type" content="article">
<meta property="og:title" content="react render阶段（completeWork）">
<meta property="og:url" content="https://anca.gitee.io/2020/11/22/reactRenderCompleteWork/index.html">
<meta property="og:site_name" content="孙杰的博客">
<meta property="og:description" content="Fiber节点是如何被创建并构建Fiber树之completeWork做的事？">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://anca.gitee.io/jiesun.gitee.io/images/fiber.jpg">
<meta property="article:published_time" content="2020-11-22T11:40:48.000Z">
<meta property="article:modified_time" content="2023-08-26T16:04:17.158Z">
<meta property="article:author" content="jiesun">
<meta property="article:tag" content="技术博客">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://anca.gitee.io/jiesun.gitee.io/images/fiber.jpg">
    
    
    <!--- Icon Part-->
    <link rel="icon" type="image/png" href="/jiesun.gitee.io/images/redefine-favicon.svg" sizes="192x192">
    <link rel="apple-touch-icon" sizes="180x180" href="/jiesun.gitee.io/images/redefine-favicon.svg">
    <meta name="theme-color" content="#A31F34">
    <link rel="shortcut icon" href="/jiesun.gitee.io/images/redefine-favicon.svg">
    <!--- Page Info-->
    
    <title>
        
            react render阶段（completeWork） -
        
        极乐净土
    </title>
    
<link rel="stylesheet" href="/jiesun.gitee.io/css/style.css">


    
        
<link rel="stylesheet" href="/jiesun.gitee.io/css/build/styles.css">

    

    
<link rel="stylesheet" href="/jiesun.gitee.io/fonts/fonts.css">

    
<link rel="stylesheet" href="/jiesun.gitee.io/fonts/Satoshi/satoshi.css">

    
<link rel="stylesheet" href="/jiesun.gitee.io/fonts/Chillax/chillax.css">

    <!--- Font Part-->
    
    
    
    

    <!--- Inject Part-->
    
    <script id="hexo-configurations">
    let Global = window.Global || {};
    Global.hexo_config = {"hostname":"anca.gitee.io","root":"/jiesun.gitee.io/","language":"zh-CN"};
    Global.theme_config = {"articles":{"style":{"font_size":"16px","line_height":1.5,"image_border_radius":"14px","image_alignment":"center","image_caption":false,"link_icon":true},"word_count":{"enable":true,"count":true,"min2read":true},"author_label":{"enable":true,"auto":false,"list":[]},"code_block":{"copy":true,"style":"mac","font":{"enable":false,"family":null,"url":null}},"toc":{"enable":true,"max_depth":3,"number":false,"expand":true,"init_open":true},"copyright":true,"lazyload":true,"recommendation":{"enable":false,"title":"推荐阅读","limit":3,"mobile_limit":2,"placeholder":"/images/wallhaven-wqery6-light.webp","skip_dirs":[]}},"colors":{"primary":"#A31F34","secondary":null},"global":{"fonts":{"chinese":{"enable":false,"family":null,"url":null},"english":{"enable":false,"family":null,"url":null}},"content_max_width":"1000px","sidebar_width":"210px","hover":{"shadow":true,"scale":false},"scroll_progress":{"bar":false,"percentage":true},"website_counter":{"url":"https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js","enable":true,"site_pv":true,"site_uv":true,"post_pv":true},"single_page":true,"open_graph":true,"google_analytics":{"enable":false,"id":null}},"home_banner":{"enable":true,"style":"fixed","image":{"light":"/images/wallhaven-wqery6-light.webp","dark":"/images/wallhaven-wqery6-dark.webp"},"title":"极乐净土","subtitle":{"text":[],"hitokoto":{"enable":false,"api":"https://v1.hitokoto.cn"},"typing_speed":100,"backing_speed":80,"starting_delay":500,"backing_delay":1500,"loop":true,"smart_backspace":true},"text_color":{"light":"#fff","dark":"#d1d1b6"},"text_style":{"title_size":"2.8rem","subtitle_size":"1.5rem","line_height":1.2},"custom_font":{"enable":false,"family":null,"url":null},"social_links":{"enable":false,"links":{"github":null,"instagram":null,"zhihu":null,"twitter":null,"email":null},"qrs":{"weixin":null}}},"plugins":{"feed":{"enable":false},"aplayer":{"enable":true,"type":"fixed","audios":[{"name":"起风了","artist":"买辣椒也用券","url":"https://music.163.com/song/media/outer/url?id=1330348068.mp3","cover":"https://p2.music.126.net/diGAyEmpymX8G7JcnElncQ==/109951163699673355.jpg?param=130y130"},{"name":"盗墓笔记·十年人间","artist":"青肆ToriDo","url":"https://music.163.com/song/media/outer/url?id=1298484254.mp3","cover":"https://p2.music.126.net/4vY0tPpnINMLzjV8UKywIg==/109951163438805808.jpg?param=130y130"},{"name":"囍","artist":"葛东琪","url":"https://music.163.com/song/media/outer/url?id=1303289043.mp3","cover":"https://p2.music.126.net/H3Q3SeMVtuHvHY2uyaQdOw==/109951163472855051.jpg?param=130y130"}]},"mermaid":{"enable":false,"version":"9.3.0"}},"version":"2.4.4","navbar":{"auto_hide":false,"color":{"left":"#f78736","right":"#367df7","transparency":35},"links":{"Home":{"path":"/","icon":"fa-regular fa-house"},"Memos":{"path":"/essay","icon":"fa-regular fa-lightbulb"},"Archives":{"icon":"fa-regular fa-archive","submenus":{"Archives":"/archives","Categories":"/categories","Tags":"/tags"}}},"search":{"enable":false,"preload":true}},"page_templates":{"friends_column":2,"tags_style":"blur"},"home":{"sidebar":{"enable":true,"position":"left","first_item":"menu","announcement":null,"links":null},"article_date_format":"auto","categories":{"enable":true,"limit":3},"tags":{"enable":true,"limit":3}},"footerStart":"2022/8/17 11:45:14"};
    Global.language_ago = {"second":"%s 秒前","minute":"%s 分钟前","hour":"%s 小时前","day":"%s 天前","week":"%s 周前","month":"%s 个月前","year":"%s 年前"};
    Global.data_config = {"masonry":false};
  </script>
    
    <!--- Fontawesome Part-->
    
<link rel="stylesheet" href="/jiesun.gitee.io/fontawesome/fontawesome.min.css">

    
<link rel="stylesheet" href="/jiesun.gitee.io/fontawesome/brands.min.css">

    
<link rel="stylesheet" href="/jiesun.gitee.io/fontawesome/solid.min.css">

    
<link rel="stylesheet" href="/jiesun.gitee.io/fontawesome/regular.min.css">

    
    
    
    
<meta name="generator" content="Hexo 6.3.0"></head>


<body>
<div class="progress-bar-container">
    

    
        <span class="pjax-progress-bar"></span>
        <span class="swup-progress-icon">
            <i class="fa-solid fa-circle-notch fa-spin"></i>
        </span>
    
</div>


<main class="page-container" id="swup">

    

    <div class="main-content-container">


        <div class="main-content-header">
            <header class="navbar-container">
    
    <div class="navbar-content">
        <div class="left">
            
            <a class="logo-title" href="https://anca.gitee.io/">
                
                极乐净土
                
            </a>
        </div>

        <div class="right">
            <!-- PC -->
            <div class="desktop">
                <ul class="navbar-list">
                    
                        
                            <li class="navbar-item">
                                <!-- Menu -->
                                <a class="" 
                                    href="/jiesun.gitee.io/"  >
                                    
                                        
                                            <i class="fa-regular fa-house"></i>
                                        
                                        首页
                                    
                                </a>
                                <!-- Submenu -->
                                
                            </li>
                    
                        
                            <li class="navbar-item">
                                <!-- Menu -->
                                <a class="" 
                                    href="/jiesun.gitee.io/essay"  >
                                    
                                        
                                            <i class="fa-regular fa-lightbulb"></i>
                                        
                                        MEMOS
                                    
                                </a>
                                <!-- Submenu -->
                                
                            </li>
                    
                        
                            <li class="navbar-item">
                                <!-- Menu -->
                                <a class="has-dropdown" 
                                    href="#" onClick="return false;">
                                    
                                        
                                            <i class="fa-regular fa-archive"></i>
                                        
                                        归档&nbsp;<i class="fa-solid fa-chevron-down"></i>
                                    
                                </a>
                                <!-- Submenu -->
                                
                                    <ul class="sub-menu">
                                    
                                        <li>
                                        <a href="/jiesun.gitee.io/archives">归档
                                        </a>
                                        </li>
                                    
                                        <li>
                                        <a href="/jiesun.gitee.io/categories">分类
                                        </a>
                                        </li>
                                    
                                        <li>
                                        <a href="/jiesun.gitee.io/tags">标签
                                        </a>
                                        </li>
                                    
                                    </ul>
                                
                            </li>
                    
                    
                </ul>
            </div>
            <!-- Mobile -->
            <div class="mobile">
                
                <div class="icon-item navbar-bar">
                    <div class="navbar-bar-middle"></div>
                </div>
            </div>
        </div>
    </div>

    <!-- Mobile drawer -->
    <div class="navbar-drawer w-full absolute top-0 left-0 bg-background-color">
        <ul class="drawer-navbar-list flex flex-col justify-start items-center">
            
                
                    <li class="drawer-navbar-item text-base my-1.5 flex justify-center items-center">
                        <a class="rounded-3xl py-1.5 px-5 hover:border hover:!text-primary active:!text-primary group " 
                        href="/jiesun.gitee.io/"  >
                             
                                
                                    <i class="fa-regular fa-house"></i>
                                
                                首页
                            
                        </a>
                    </li>
                    <!-- Submenu -->
                    
            
                
                    <li class="drawer-navbar-item text-base my-1.5 flex justify-center items-center">
                        <a class="rounded-3xl py-1.5 px-5 hover:border hover:!text-primary active:!text-primary group " 
                        href="/jiesun.gitee.io/essay"  >
                             
                                
                                    <i class="fa-regular fa-lightbulb"></i>
                                
                                MEMOS
                            
                        </a>
                    </li>
                    <!-- Submenu -->
                    
            
                
                    <li class="drawer-navbar-item text-base my-1.5 flex justify-center items-center">
                        <a class="rounded-3xl py-1.5 px-5 hover:border hover:!text-primary active:!text-primary group has-dropdown" 
                        href="#" onClick="return false;">
                            
                                
                                    <i class="fa-regular fa-archive"></i>
                                
                                归档&nbsp;<i class="group-hover:rotate-180 transition-transform fa-solid fa-chevron-down"></i>
                            
                        </a>
                    </li>
                    <!-- Submenu -->
                              
                        
                            <li class="text-base flex justify-center items-center hover:underline active:underline hover:underline-offset-1 rounded-3xl">
                                <a class="py-0.5" href="/jiesun.gitee.io/archives">归档</a>
                            </li>
                        
                            <li class="text-base flex justify-center items-center hover:underline active:underline hover:underline-offset-1 rounded-3xl">
                                <a class="py-0.5" href="/jiesun.gitee.io/categories">分类</a>
                            </li>
                        
                            <li class="text-base flex justify-center items-center hover:underline active:underline hover:underline-offset-1 rounded-3xl">
                                <a class="py-0.5" href="/jiesun.gitee.io/tags">标签</a>
                            </li>
                        
                    
            

        </ul>
    </div>

    <div class="window-mask"></div>

</header>


        </div>

        <div class="main-content-body">

            

            <div class="main-content">

                
                    <div class="post-page-container">
    <div class="article-content-container">

        <div class="article-title">
            
                
                
                <img src="https://anca.gitee.io/jiesun.gitee.io/images/bg13.jpeg" alt="react render阶段（completeWork）" class="max-w-none"/>
                
                <h1 class="article-title-cover">react render阶段（completeWork）</h1>
            
            </div>
            
                    
        
        
            <div class="article-header">
                <div class="avatar">
                    <img src="/jiesun.gitee.io/images/redefine-avatar.svg">
                </div>
                <div class="info">
                    <div class="author">
                        <span class="name">jiesun</span>
                        
                            <span class="author-label">Lv3</span>
                        
                    </div>
                    <div class="meta-info">
                        <div class="article-meta-info">
    <span class="article-date article-meta-item">
        <i class="fa-regular fa-pen-fancy"></i>&nbsp;
        <span class="desktop">2020-11-22 19:40:48</span>
        <span class="mobile">2020-11-22 19:40:48</span>
        <span class="hover-info">创建</span>
    </span>
    
        <span class="article-date article-meta-item">
            <i class="fa-regular fa-wrench"></i>&nbsp;
            <span class="desktop">2023-08-27 00:04:17</span>
            <span class="mobile">2023-08-27 00:04:17</span>
            <span class="hover-info">更新</span>
        </span>
    

    
        <span class="article-categories article-meta-item">
            <i class="fa-regular fa-folders"></i>&nbsp;
            <ul>
                
                
                    
                        
                        <li>
                            <a href="/jiesun.gitee.io/categories/react-%E6%BA%90%E7%A0%81/">react 源码</a>&nbsp;
                        </li>
                    
                    
                
            </ul>
        </span>
    
    

    
    
    
    
        <span class="article-pv article-meta-item">
            <i class="fa-regular fa-eye"></i>&nbsp;<span id="busuanzi_value_page_pv"></span>
        </span>
    
</div>

                    </div>
                </div>
            </div>
        

        


        <div class="article-content markdown-body">
            <h3 id="流程"><a href="#流程" class="headerlink" title="流程"></a>流程</h3><ul>
<li>beginWork -&gt; 从上到下生成child关系的过程</li>
<li>completeWork -&gt; 从下到上生成return关系的过程<br><img  
                     lazyload
                     src="/images/loading.svg"
                     data-src="https://anca.gitee.io/jiesun.gitee.io/images/fiber.jpg"
                     
                ></li>
</ul>
<h3 id="completeWork做的事"><a href="#completeWork做的事" class="headerlink" title="completeWork做的事"></a>completeWork做的事</h3><blockquote>
<p>从下到上生成return关系的过程 生成dom指向stateNode 并将下面的dom插入到上面dom的子节点中，最后一次性渲染到真实dom节点中</p>
</blockquote>
<div class="highlight-container" data-rel="Plaintext"><figure class="iseeu highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">completeWork(</span><br><span class="line">    current: Fiber | null,</span><br><span class="line">    workInProgress: Fiber,</span><br><span class="line">    renderLanes: Lanes,</span><br><span class="line">  ): Fiber | null</span><br><span class="line">  </span><br></pre></td></tr></table></figure></div>
<ul>
<li>current 是当前组件对应的Fiber节点对应的上次更新的Fiber节点</li>
<li>workInProgress 是这次更新组件的Fiber节点</li>
<li>renderLanes 优先级</li>
</ul>
<h2 id="begin工作内容"><a href="#begin工作内容" class="headerlink" title="begin工作内容"></a>begin工作内容</h2><ul>
<li>mount 创建dom节点</li>
<li>update 对dom props的一下修改<blockquote>
<p>HostComponent：原生DOM组件对应的Fiber节点<br>current !&#x3D;&#x3D; null &amp;&amp; workInProgress.stateNode !&#x3D; null 判断是不是update，如果上一次fiber下没有关联真实dom，这次也会去创建真实dom节点</p>
</blockquote>
<div class="highlight-container" data-rel="Plaintext"><figure class="iseeu highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br></pre></td><td class="code"><pre><span class="line">function completeWork(</span><br><span class="line">    current: Fiber | null,</span><br><span class="line">    workInProgress: Fiber,</span><br><span class="line">    renderLanes: Lanes,</span><br><span class="line">  ): Fiber | null &#123;</span><br><span class="line">    const newProps = workInProgress.pendingProps;</span><br><span class="line">  </span><br><span class="line">    switch (workInProgress.tag) &#123;</span><br><span class="line">      case HostComponent: &#123;</span><br><span class="line">        popHostContext(workInProgress);</span><br><span class="line">        const rootContainerInstance = getRootHostContainer();</span><br><span class="line">        const type = workInProgress.type;</span><br><span class="line">        if (current !== null &amp;&amp; workInProgress.stateNode != null) &#123; // update</span><br><span class="line">          updateHostComponent(</span><br><span class="line">            current,</span><br><span class="line">            workInProgress,</span><br><span class="line">            type,</span><br><span class="line">            newProps,</span><br><span class="line">            rootContainerInstance,</span><br><span class="line">          );</span><br><span class="line">  </span><br><span class="line">          if (enableDeprecatedFlareAPI) &#123;</span><br><span class="line">            const prevListeners = current.memoizedProps.DEPRECATED_flareListeners;</span><br><span class="line">            const nextListeners = newProps.DEPRECATED_flareListeners;</span><br><span class="line">            if (prevListeners !== nextListeners) &#123;</span><br><span class="line">              markUpdate(workInProgress);</span><br><span class="line">            &#125;</span><br><span class="line">          &#125;</span><br><span class="line">  </span><br><span class="line">          if (current.ref !== workInProgress.ref) &#123;</span><br><span class="line">            markRef(workInProgress);</span><br><span class="line">          &#125;</span><br><span class="line">        &#125; else &#123; // mount</span><br><span class="line">          if (!newProps) &#123;</span><br><span class="line">            invariant(</span><br><span class="line">              workInProgress.stateNode !== null,</span><br><span class="line">              &#x27;We must have new props for new mounts. This error is likely &#x27; +</span><br><span class="line">                &#x27;caused by a bug in React. Please file an issue.&#x27;,</span><br><span class="line">            );</span><br><span class="line">            return null;</span><br><span class="line">          &#125;</span><br><span class="line">  </span><br><span class="line">          const currentHostContext = getHostContext();</span><br><span class="line">          const wasHydrated = popHydrationState(workInProgress);</span><br><span class="line">          if (wasHydrated) &#123;</span><br><span class="line">            if (</span><br><span class="line">              prepareToHydrateHostInstance(</span><br><span class="line">                workInProgress,</span><br><span class="line">                rootContainerInstance,</span><br><span class="line">                currentHostContext,</span><br><span class="line">              )</span><br><span class="line">            ) &#123;</span><br><span class="line">              markUpdate(workInProgress);</span><br><span class="line">            &#125;</span><br><span class="line">            if (enableDeprecatedFlareAPI) &#123;</span><br><span class="line">              const listeners = newProps.DEPRECATED_flareListeners;</span><br><span class="line">              if (listeners != null) &#123;</span><br><span class="line">                updateDeprecatedEventListeners(</span><br><span class="line">                  listeners,</span><br><span class="line">                  workInProgress,</span><br><span class="line">                  rootContainerInstance,</span><br><span class="line">                );</span><br><span class="line">              &#125;</span><br><span class="line">            &#125;</span><br><span class="line">          &#125; else &#123;</span><br><span class="line">            const instance = createInstance(</span><br><span class="line">              type,</span><br><span class="line">              newProps,</span><br><span class="line">              rootContainerInstance,</span><br><span class="line">              currentHostContext,</span><br><span class="line">              workInProgress,</span><br><span class="line">            );</span><br><span class="line">  </span><br><span class="line">            appendAllChildren(instance, workInProgress, false, false);</span><br><span class="line">  </span><br><span class="line">            workInProgress.stateNode = instance;</span><br><span class="line">  </span><br><span class="line">            if (enableDeprecatedFlareAPI) &#123;</span><br><span class="line">              const listeners = newProps.DEPRECATED_flareListeners;</span><br><span class="line">              if (listeners != null) &#123;</span><br><span class="line">                updateDeprecatedEventListeners(</span><br><span class="line">                  listeners,</span><br><span class="line">                  workInProgress,</span><br><span class="line">                  rootContainerInstance,</span><br><span class="line">                );</span><br><span class="line">              &#125;</span><br><span class="line">            &#125;</span><br><span class="line">  </span><br><span class="line">            if (</span><br><span class="line">              finalizeInitialChildren(</span><br><span class="line">                instance,</span><br><span class="line">                type,</span><br><span class="line">                newProps,</span><br><span class="line">                rootContainerInstance,</span><br><span class="line">                currentHostContext,</span><br><span class="line">              )</span><br><span class="line">            ) &#123;</span><br><span class="line">              markUpdate(workInProgress);</span><br><span class="line">            &#125;</span><br><span class="line">          &#125;</span><br><span class="line">  </span><br><span class="line">          if (workInProgress.ref !== null) &#123;</span><br><span class="line">            markRef(workInProgress);</span><br><span class="line">          &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        return null;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br></pre></td></tr></table></figure></div></li>
</ul>
<h1 id="update"><a href="#update" class="headerlink" title="update"></a>update</h1><p>处理已存在的dom 的 <code>props</code></p>
<ul>
<li>onClick、onChange等回调函数的注册</li>
<li>处理style prop</li>
<li>处理DANGEROUSLY_SET_INNER_HTML prop</li>
<li>处理children prop<br>其中最主要的是updatePayload方法<br>将变化的东西插入到 <code>workInProgress.updateQueue</code> 到commit阶段处理</li>
</ul>
<p>workInProgress.updateQueue &#x3D; updatePayload</p>
<div class="highlight-container" data-rel="Plaintext"><figure class="iseeu highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">const updatePayload = prepareUpdate(</span><br><span class="line">   instance,</span><br><span class="line">   type,</span><br><span class="line">   oldProps,</span><br><span class="line">   newProps,</span><br><span class="line">   rootContainerInstance,</span><br><span class="line">   currentHostContext,</span><br><span class="line"> );</span><br><span class="line"> // TODO: Type this specific to this type of component.</span><br><span class="line"> workInProgress.updateQueue = (updatePayload: any);</span><br></pre></td></tr></table></figure></div>

<h1 id="mount"><a href="#mount" class="headerlink" title="mount"></a>mount</h1><ul>
<li>生成dom节点挂在stateNode下</li>
<li>将子节点的dom 挂载到父节点的dom上</li>
<li>和update一样处理dom的props<div class="highlight-container" data-rel="Plaintext"><figure class="iseeu highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line">const instance = createInstance(</span><br><span class="line">            type,</span><br><span class="line">            newProps,</span><br><span class="line">            rootContainerInstance,</span><br><span class="line">            currentHostContext,</span><br><span class="line">            workInProgress,</span><br><span class="line">          );</span><br><span class="line"></span><br><span class="line">          appendAllChildren(instance, workInProgress, false, false);</span><br><span class="line"></span><br><span class="line">          // This needs to be set before we mount Flare event listeners</span><br><span class="line">          workInProgress.stateNode = instance;</span><br><span class="line"></span><br><span class="line">          // Certain renderers require commit-time effects for initial mount.</span><br><span class="line">          // (eg DOM renderer supports auto-focus for certain elements).</span><br><span class="line">          // Make sure such renderers get scheduled for later work.</span><br><span class="line">          if (</span><br><span class="line">            finalizeInitialChildren(</span><br><span class="line">              instance,</span><br><span class="line">              type,</span><br><span class="line">              newProps,</span><br><span class="line">              rootContainerInstance,</span><br><span class="line">              currentHostContext,</span><br><span class="line">            )</span><br><span class="line">          ) &#123;</span><br><span class="line">            markUpdate(workInProgress);</span><br><span class="line">          &#125;</span><br><span class="line">        &#125;</span><br></pre></td></tr></table></figure></div>
通过appendAllChildren，在完成所有的return后， 会构建一个内存的dom树</li>
</ul>
<h2 id="effectList-单向链表"><a href="#effectList-单向链表" class="headerlink" title="effectList 单向链表"></a>effectList 单向链表</h2><blockquote>
<p>存在effectTag的节点会存在<code>effectList</code>，在commit阶段执行</p>
</blockquote>
<div class="highlight-container" data-rel="Plaintext"><figure class="iseeu highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">if (</span><br><span class="line">      returnFiber !== null &amp;&amp;</span><br><span class="line">      (returnFiber.effectTag &amp; Incomplete) === NoEffect</span><br><span class="line">    ) &#123;</span><br><span class="line">      if (returnFiber.firstEffect === null) &#123;</span><br><span class="line">        returnFiber.firstEffect = completedWork.firstEffect;</span><br><span class="line">      &#125;</span><br><span class="line">      if (completedWork.lastEffect !== null) &#123;</span><br><span class="line">        if (returnFiber.lastEffect !== null) &#123;</span><br><span class="line">          returnFiber.lastEffect.nextEffect = completedWork.firstEffect;</span><br><span class="line">        &#125;</span><br><span class="line">        returnFiber.lastEffect = completedWork.lastEffect;</span><br><span class="line">      &#125;</span><br><span class="line">      const effectTag = completedWork.effectTag;</span><br><span class="line">      if (effectTag &gt; PerformedWork) &#123;</span><br><span class="line">        if (returnFiber.lastEffect !== null) &#123;</span><br><span class="line">          returnFiber.lastEffect.nextEffect = completedWork;</span><br><span class="line">        &#125; else &#123;</span><br><span class="line">          returnFiber.firstEffect = completedWork;</span><br><span class="line">        &#125;</span><br><span class="line">        returnFiber.lastEffect = completedWork;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br></pre></td></tr></table></figure></div>

        </div>

        
            <div class="post-copyright-info">
                <div class="article-copyright-info-container">
    <ul>
        <li><strong>标题:</strong> react render阶段（completeWork）</li>
        <li><strong>作者:</strong> jiesun</li>
        <li><strong>创建于
                :</strong> 2020-11-22 19:40:48</li>
        
            <li>
                <strong>更新于
                    :</strong> 2023-08-27 00:04:17
            </li>
        
        <li>
            <strong>链接:</strong> https://anca.gitee.io/jiesun.gitee.io/2020/11/22/reactRenderCompleteWork/
        </li>
        <li>
            <strong>
                版权声明:
            </strong>
            
            本文章采用 <a class="license" target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh">CC BY-NC-SA 4.0</a> 进行许可。
            

        </li>
    </ul>
</div>

            </div>
        

        

        

        
            <div class="article-nav">
                
                    <div class="article-prev">
                        <a class="prev"
                        rel="prev"
                        href="/jiesun.gitee.io/2020/11/22/reactRenderBeginWork/"
                        >
                            <span class="left arrow-icon flex justify-center items-center">
                                <i class="fa-solid fa-chevron-left"></i>
                            </span>
                            <span class="title flex justify-center items-center">
                                <span class="post-nav-title-item">react render阶段（beginWork）</span>
                                <span class="post-nav-item">上一篇</span>
                            </span>
                        </a>
                    </div>
                
                
                    <div class="article-next">
                        <a class="next"
                        rel="next"
                        href="/jiesun.gitee.io/2020/10/12/reactFiber/"
                        >
                            <span class="title flex justify-center items-center">
                                <span class="post-nav-title-item">react Fiber</span>
                                <span class="post-nav-item">下一篇</span>
                            </span>
                            <span class="right arrow-icon flex justify-center items-center">
                                <i class="fa-solid fa-chevron-right"></i>
                            </span>
                        </a>
                    </div>
                
            </div>
        


        
    </div>

    
        <div class="toc-content-container">
            <div class="post-toc-wrap">
    <div class="post-toc">
        <div class="toc-title">此页目录</div>
        <div class="page-title">react render阶段（completeWork）</div>
        <ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%B5%81%E7%A8%8B"><span class="nav-text">流程</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#completeWork%E5%81%9A%E7%9A%84%E4%BA%8B"><span class="nav-text">completeWork做的事</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#begin%E5%B7%A5%E4%BD%9C%E5%86%85%E5%AE%B9"><span class="nav-text">begin工作内容</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#update"><span class="nav-text">update</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#mount"><span class="nav-text">mount</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#effectList-%E5%8D%95%E5%90%91%E9%93%BE%E8%A1%A8"><span class="nav-text">effectList 单向链表</span></a>

    </div>
</div>
        </div>
    
</div>



                

            </div>

            

        </div>

        <div class="main-content-footer">
            <footer class="footer mt-5 py-5 h-auto text-base text-third-text-color relative border-t-2 border-t-border-color">
    <div class="info-container py-3 text-center">
        
        <div class="text-center">
            &copy;
            
              <span>2022</span>
              -
            
            2023&nbsp;&nbsp;<i class="fa-solid fa-heart fa-beat" style="--fa-animation-duration: 0.5s; color: #f54545"></i>&nbsp;&nbsp;<a href="/">jiesun</a>
        </div>
        
            <script data-swup-reload-script src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
            <div class="relative text-center lg:absolute lg:right-[20px] lg:top-1/2 lg:-translate-y-1/2 lg:text-right">
                
                    <span id="busuanzi_container_site_uv" class="lg:!block">
                        <span class="text-sm">访问人数</span>
                        <span id="busuanzi_value_site_uv"></span>
                    </span>
                
                
                    <span id="busuanzi_container_site_pv" class="lg:!block">
                        <span class="text-sm">总访问量</span>
                        <span id="busuanzi_value_site_pv"></span>
                    </span>
                
            </div>
        
        <div class="relative text-center lg:absolute lg:left-[20px] lg:top-1/2 lg:-translate-y-1/2 lg:text-left">
            <span class="lg:block text-sm">由 <?xml version="1.0" encoding="utf-8"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg class="relative top-[2px] inline-block align-baseline" version="1.1" id="圖層_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="1rem" height="1rem" viewBox="0 0 512 512" enable-background="new 0 0 512 512" xml:space="preserve"><path fill="#0E83CD" d="M256.4,25.8l-200,115.5L56,371.5l199.6,114.7l200-115.5l0.4-230.2L256.4,25.8z M349,354.6l-18.4,10.7l-18.6-11V275H200v79.6l-18.4,10.7l-18.6-11v-197l18.5-10.6l18.5,10.8V237h112v-79.6l18.5-10.6l18.5,10.8V354.6z"/></svg><a target="_blank" class="text-base" href="https://hexo.io">Hexo</a> 驱动</span>
            <span class="text-sm lg:block">主题&nbsp;<a class="text-base" target="_blank" href="https://github.com/EvanNotFound/hexo-theme-redefine">Redefine v2.4.4</a></span>
        </div>
        
        
            <div>
                博客已运行 <span class="odometer" id="runtime_days" ></span> 天 <span class="odometer" id="runtime_hours"></span> 小时 <span class="odometer" id="runtime_minutes"></span> 分钟 <span class="odometer" id="runtime_seconds"></span> 秒
            </div>
        
        
            <script data-swup-reload-script>
                try {
                    function odometer_init() {
                    const elements = document.querySelectorAll('.odometer');
                    elements.forEach(el => {
                        new Odometer({
                            el,
                            format: '( ddd).dd',
                            duration: 200
                        });
                    });
                    }
                    odometer_init();
                } catch (error) {}
            </script>
        
        
        
    </div>  
</footer>
        </div>
    </div>

    
        <div class="post-tools">
            <div class="post-tools-container">
    <ul class="article-tools-list">
        <!-- TOC aside toggle -->
        
            <li class="right-bottom-tools page-aside-toggle">
                <i class="fa-regular fa-outdent"></i>
            </li>
        

        <!-- go comment -->
        
    </ul>
</div>

        </div>
    

    <div class="right-side-tools-container">
        <div class="side-tools-container">
    <ul class="hidden-tools-list">
        <li class="right-bottom-tools tool-font-adjust-plus flex justify-center items-center">
            <i class="fa-regular fa-magnifying-glass-plus"></i>
        </li>

        <li class="right-bottom-tools tool-font-adjust-minus flex justify-center items-center">
            <i class="fa-regular fa-magnifying-glass-minus"></i>
        </li>

        <li class="right-bottom-tools tool-expand-width flex justify-center items-center">
            <i class="fa-regular fa-expand"></i>
        </li>

        <li class="right-bottom-tools tool-dark-light-toggle flex justify-center items-center">
            <i class="fa-regular fa-moon"></i>
        </li>

        <!-- rss -->
        

        

        <li class="right-bottom-tools tool-scroll-to-bottom flex justify-center items-center">
            <i class="fa-regular fa-arrow-down"></i>
        </li>
    </ul>

    <ul class="visible-tools-list">
        <li class="right-bottom-tools toggle-tools-list flex justify-center items-center">
            <i class="fa-regular fa-cog fa-spin"></i>
        </li>
        
            <li class="right-bottom-tools tool-scroll-to-top flex justify-center items-center">
                <i class="arrow-up fas fa-arrow-up"></i>
                <span class="percent"></span>
            </li>
        
        
    </ul>
</div>

    </div>

    <div class="image-viewer-container">
    <img src="">
</div>


    

</main>


    
<script src="/jiesun.gitee.io/js/libs/Swup.min.js"></script>

<script src="/jiesun.gitee.io/js/libs/SwupSlideTheme.min.js"></script>

<script src="/jiesun.gitee.io/js/libs/SwupScriptsPlugin.min.js"></script>

<script src="/jiesun.gitee.io/js/libs/SwupProgressPlugin.min.js"></script>

<script src="/jiesun.gitee.io/js/libs/SwupScrollPlugin.min.js"></script>

<script>
    const swup = new Swup({
        plugins: [
            new SwupScriptsPlugin({
                optin: true,
            }),
            new SwupProgressPlugin(),
            new SwupScrollPlugin({
                offset: 80,
            }),
            new SwupSlideTheme({
                mainElement: ".main-content-body",
            }),
        ],
        containers: ["#swup"],
    });

    swup.hooks.on("page:view", () => {
        Global.refresh();
    });

    // if (document.readyState === "complete") {
    //
    // } else {
    //     document.addEventListener("DOMContentLoaded", () => init());
    // }
</script>






<script src="/jiesun.gitee.io/js/utils.js" type="module"></script>

<script src="/jiesun.gitee.io/js/main.js" type="module"></script>

<script src="/jiesun.gitee.io/js/layouts/navbarShrink.js" type="module"></script>

<script src="/jiesun.gitee.io/js/tools/scrollTopBottom.js" type="module"></script>

<script src="/jiesun.gitee.io/js/tools/lightDarkSwitch.js" type="module"></script>

<script src="/jiesun.gitee.io/js/layouts/categoryList.js" type="module"></script>





    
<script src="/jiesun.gitee.io/js/tools/codeBlock.js"></script>




    
<script src="/jiesun.gitee.io/js/layouts/lazyload.js"></script>




    
<script src="/jiesun.gitee.io/js/tools/runtime.js"></script>

    
<script src="/jiesun.gitee.io/js/libs/odometer.min.js"></script>

    
<link rel="stylesheet" href="/jiesun.gitee.io/assets/odometer-theme-minimal.css">




  
<script src="/jiesun.gitee.io/js/libs/Typed.min.js"></script>

  
<script src="/jiesun.gitee.io/js/plugins/typed.js"></script>







<div class="post-scripts" data-swup-reload-script>
    
        
<script src="/jiesun.gitee.io/js/libs/anime.min.js"></script>

        
<script src="/jiesun.gitee.io/js/tools/tocToggle.js" type="module"></script>

<script src="/jiesun.gitee.io/js/layouts/toc.js" type="module"></script>

<script src="/jiesun.gitee.io/js/plugins/tabs.js" type="module"></script>

    
</div>


    <div id="aplayer"></div>

<script src="/jiesun.gitee.io/js/libs/APlayer.min.js"></script>


<script src="/jiesun.gitee.io/js/plugins/aplayer.js"></script>


</body>
</html>
